<template>
  <div class="sharedetail">
    <van-nav-bar>
      <template #left>
        <i @click="goBack" class="icon iconfont iconbtn_nav_back leftArro"></i>
      </template>
    </van-nav-bar>
    <div class="articel" v-if="article.author != {}">
      <div class="artiTitle">
        <h3>{{ article.title }}</h3>
      </div>
      <div class="author">
        <div class="myImg">
          <img :src="article.author.avatar" alt="" />
        </div>
        <div>
          <div class="name">{{ article.author.nickname }}</div>
          <div class="time">{{ article.updated_at | dateformat }}</div>
        </div>
      </div>
      <div class="content"><span v-html="article.content"></span></div>
    </div>
    <div class="comment">
      <div class="commentTitle"><span>评论</span><sup>68</sup></div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="comArea" v-for="(item, index) in commentList" :key="index">
          <div class="userRow" v-if="item.content != ''">
            <div class="info">
              <div class="myImg">
                <img
                  @click="replyEnvent(item, index)"
                  :src="'http://localhost:1337' + item.author.avatar"
                  alt=""
                />
              </div>
              <div>
                <div class="name">{{ item.author.nickname }}</div>
                <div class="time">{{ item.created_at | dateformat }}</div>
              </div>
            </div>
            <div>0 <van-icon name="good-job-o" size="30" /></div>
          </div>
          <div class="main">
            <div class="comContent" v-if="item.content != ''">
              {{ item.content }}
            </div>
            <div v-if="item.children_comments.length !== 0">
              <div
                class="reply"
                v-for="(subitem, subindex) in item.children_comments"
                :key="subindex"
              >
                <span>{{ subitem.author }}:</span>
                <span>{{ subitem.content }}</span>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 书写评论区域 -->
    <div class="write">
      <div class="ipt" @click="popEvent">我来补充两句</div>
      <div class="star">
        <van-icon
          color="red"
          v-if="isCollect"
          size="24"
          name="star-o"
          @click="starClick"
        />
        <van-icon v-else size="24" name="star-o" @click="starClick" />
        <span>{{ article.collect }}</span>
      </div>
      <div class="star">
        <van-icon size="24" name="good-job-o" />
        <span>1</span>
      </div>
      <div class="star">
        <van-icon size="24" name="share-o" />
        <span>1</span>
      </div>
    </div>
    <van-popup v-model="isshow" position="bottom" :style="{ height: '30%' }">
      <div class="field">
        <van-field
          v-model="comText"
          rows="4"
          type="textarea"
          :placeholder="placeholder"
        />
        <div class="send" @click="addCom">发送</div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { getLocal } from '@/utils/local'
import {
  apiGetArticel,
  apiGetComment,
  apiAddComment,
  apiCollectArt
} from '@/api/find'
export default {
  computed: {
    ...mapState({
      userInfo: state => state.userInfo
    }),
    // 是否被收藏
    isCollect: function () {
      return (
        this.userInfo.collectArticles &&
        this.userInfo.collectArticles.includes(+this.id)
      )
    }
  },
  data () {
    return {
      id: this.$route.params.id,
      article: {
        author: {}
      },
      commentList: [],
      //   从几条数据开始申请
      start: 0,
      //  申请多少题数据
      limit: 5,
      //   list 是否加载中
      loading: false,
      //  list 数据是否加载完成
      finished: false,
      //   是否显示模板
      isshow: false,
      //   评论文本
      comText: '',
      //   判断是否处于回复评论状态
      isreply: false,
      // 输入框内置文本
      placeholder: '请输入评论数据',
      //   要回复评论的id
      comId: '',
      //   回复评论的对象
      comObj: ''
    }
  },
  methods: {
    goBack () {
      this.$router.go(-1)
    },
    // 获取文章
    async getArticle () {
      const res = await apiGetArticel(this.id)
      res.data.author.avatar = 'http://localhost:1337' + res.data.author.avatar
      this.article = res.data
      console.log('文章:', this.article)
    },
    // 获取评论列表 ---  上拉加载列表
    // 上拉加载列表
    async onLoad () {
      const res = await apiGetComment({
        id: this.id,
        limit: this.limit,
        start: this.start
      })
      //   console.log(res)

      this.commentList.push(...res.data.list)
      // console.log('评论列表', this.commentList)

      this.loading = false
      this.start += this.limit
      if (this.commentList.length >= res.data.total) {
        this.finished = true
      }
    },

    // 发送按钮发送数据
    async addCom () {
      if (getLocal('token')) {
        if (this.isreply === false) {
          const res = await apiAddComment({
            content: this.comText,
            article: this.id
          })
          this.commentList.unshift(res.data)
          //   console.log('评论数组:', this.commentList)
        } else {
          const res = await apiAddComment({
            content: this.comText,
            parent: this.comId
          })
          // this.commentList[this.index].children_comments.unshift(res.data)
          this.comObj.children_comments.push(res.data)
          // console.log(res)
        }
        this.isshow = false
        this.comText = ''
      } else {
        this.$router.push(`/login?_redirect=/sharedetail/${this.id}`)
      }
    },
    // 回复评论事件
    replyEnvent (item) {
      //   console.log('回复评论:', item)

      this.isshow = true
      this.comId = item.id
      this.placeholder = `回复:${item.author.nickname}`
      this.isreply = true
      this.comObj = item
    },
    // 评论时间
    popEvent () {
      this.isshow = true
      this.isreply = false
      this.placeholder = '请输入评论数据'
    },
    // 点击收藏
    async starClick () {
      if (this.userInfo) {
        const res = await apiCollectArt(this.id)
        this.article.collect = res.data.num
        this.userInfo.collectArticles = res.data.list
        if (this.isCollect) {
          this.$toast.success('收藏成功')
        } else {
          this.$toast.success('取消收藏成功')
        }
        // console.log('收藏', res)
      } else {
        this.$router.push('/login?_redirect=' + this.$route.fullPath)
      }
    }
  },
  created () {
    this.getArticle()
    // console.log(this.id)
    // 获取评论
    console.log('用户信息:', this.userInfo)
    // console.log('id:', this.id)
  }
}
</script>
<style lang="less" scoped>
.sharedetail {
  font-size: 12px;
  margin-bottom: 60px;
  .articel {
    padding: 0 15px;
    .artiTitle h3 {
      font-size: 18px;
      font-weight: normal;
    }
    .author {
      display: flex;
      .myImg img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-right: 10px;
      }
      .time {
        color: #ccc;
      }
    }
    .content {
      margin: 50px 0;
      line-height: 18px;
      /deep/ img {
        width: 100%;
      }
    }
  }
  .comment {
    padding: 0 15px;
    .reply {
      padding: 10px;
    }
    .commentTitle {
      span {
        font-size: 18px;
      }
      sup {
        color: #666;
      }
    }
    .userRow {
      display: flex;
      margin: 15px 0;
      .info {
        flex: 1;
        display: flex;
      }
      .myImg img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-right: 10px;
      }
      .time {
        color: #ccc;
      }
    }
    .main {
      padding-left: 50px;
      .reply {
        padding: 10px 0;
        background-color: #eee;
      }
      .comContent {
        margin-bottom: 10px;
      }
    }
  }
  .bottomBar {
    display: flex;
    padding: 15px 15px 0;
    position: fixed;
    bottom: 0;
    left: 0;
    padding-bottom: 40px;
    background-color: #fff;
    .tosay {
      flex: 1;
      padding: 7px;
      background-color: #f7f4f5;
      width: 230px;
      color: #666;
    }
    .icon {
      margin-left: 20px;
      text-align: center;
    }
  }
  /deep/ .van-nav-bar__left {
    padding: 0;
  }
  .leftArro {
    font-size: 44px;
  }
  textarea {
    box-sizing: border-box;
    padding: 0 15px;
    height: 85px;
    background-color: #f7f4f5;
  }
  .write {
    display: flex;
    padding: 10px 15px 20px;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    background-color: #fff;
    .ipt {
      flex: 1;
      background-color: #eee;
      height: 40px;
      line-height: 40px;
      padding-left: 5px;
    }
    .star {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-left: 20px;
    }
  }
  .field {
    padding: 0 15px;
    margin-top: 20px;
    /deep/.van-field__control {
      background-color: #eee;
    }
  }
  .send {
    text-align: right;
    margin-top: 10px;
    font-size: 16px;
  }
}
</style>
